/**
 *****************************************************************************
 * DESC ：. 
 * Author : Junwei Zhang 
 * CREATEDATE ：2012-7-22 
 * MODIFYLIST ：       	Name 			 Date 			Reason/Contents
 * 				------------------------------------------------------ 
 * 				 	Junwei Zhang     2012-7-22		Created.
 ****************************************************************************
 **/
package com.me.client;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import com.me.client.JTalkClient.MemberData;

/**
 * 
 * <br/>
 * 下午6:04:29 2012-7-22
 * 
 * @Author <a href="mailto:mestudying@sina.com">Junwei Zhang</a><br/>
 **/
public class JTalkFrontPopup extends JPopupMenu implements JTalkClientCommandInterface {

	private JTalkClientFront parent = null;
	private JMenuItem chatItem = null;
	private JMenuItem closeRoomItem = null;
	private JMenuItem addRoomUserItem = null;
	private JMenuItem removeRoomUserItem = null;
	private MemberData selectedUser = null;

	public JTalkFrontPopup(JTalkClientFront parent) {
		// TODO Auto-generated constructor stub
		this.parent = parent;
		this.initComponents();
		this.parent.theClient.registerForCommandResponse(this);
	}

	private void initComponents() {
		// TODO Auto-generated method stub
		this.chatItem = new JMenuItem("Open Chat Window");
		this.chatItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showChat();
			}
		});
		this.add(this.chatItem);

		this.closeRoomItem = new JMenuItem("Close Chatroom");
		this.closeRoomItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				closeChatroom();
			}
		});
		this.closeRoomItem.setVisible(false);
		this.add(this.closeRoomItem);

		this.addRoomUserItem = new JMenuItem("Add user to this chatroom");
		this.addRoomUserItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addChatroomUser();
			}
		});
		this.addRoomUserItem.setVisible(false);
		this.add(this.addRoomUserItem);

		this.removeRoomUserItem = new JMenuItem("Removes user from this chatroom");
		this.removeRoomUserItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				removeChatroomUser();
			}
		});
		this.removeRoomUserItem.setVisible(false);
		this.add(this.removeRoomUserItem);
	}

	@Override
	public void show(Component invoker, int x, int y) {
		this.selectedUser = this.parent.getSelectedUser();
		if (this.selectedUser.type.equals(JTalkParser.USER_TYPE_CHATROOM)) {
			this.closeRoomItem.setVisible(true);
			this.addRoomUserItem.setVisible(true);
			this.removeRoomUserItem.setVisible(true);
		} else {
			this.closeRoomItem.setVisible(false);
			this.addRoomUserItem.setVisible(false);
			this.removeRoomUserItem.setVisible(false);
		}
		super.show(invoker, x, y);
	}

	/**
	 * 显示聊天窗口.
	 */
	private void showChat() {
		this.parent.openChatWindow();
	}

	/**
	 * 关闭聊天室.
	 */
	private void closeChatroom() {
		this.parent.removeChatroom(this.selectedUser.short_name);
	}

	/**
	 * 向聊天室内添加一个用户.
	 */
	private void addChatroomUser() {
		this.parent.theClient.sendMessage("std::getAllUsers()", null, "command", null, "getallusers01");
	}

	/**
	 * 从聊天室内删除一个用户.
	 */
	private void removeChatroomUser() {
		this.parent.theClient.sendMessage("std::getChatRoomUsers(\"" + this.selectedUser.short_name + "\")", null,
				"command", null, "getchatroomusers01");
	}

	@Override
	public boolean setCommandAnswer(String messageID, String sender, String content) {
		// TODO Auto-generated method stub
		if ("getallusers01".equals(messageID)) {
			String lastResponse = content;
			if (lastResponse == null) {
				return false;
			}
			String[] users = lastResponse.split(";;");
			String selectedUser = (String) JOptionPane.showInputDialog(this.parent, "User name:", "Add User",
					JOptionPane.INFORMATION_MESSAGE, null, users, null);
			if (selectedUser == null) {
				return false;
			}
			this.parent.addChatroomUser(this.selectedUser.short_name, selectedUser);
			return true;
		} else if ("addchatuser01".equals(messageID)) {
			JOptionPane.showMessageDialog(this.parent, content, "Server Answer", JOptionPane.INFORMATION_MESSAGE);
		} else if ("getchatroomusers01".equals(messageID)) {
			String lastResponse = content;
			if (lastResponse == null) {
				return false;
			}
			String[] users = lastResponse.split(";;");
			String selectedUser = (String) JOptionPane.showInputDialog(this.parent, "User name:", "Remove User",
					JOptionPane.INFORMATION_MESSAGE, null, users, null);
			if(selectedUser == null){
				return false;
			}
			this.parent.removeChatroomUser(this.selectedUser.short_name, selectedUser);
			return true;
		} else if("rmchat01".equals(messageID)){
			JOptionPane.showMessageDialog(this.parent, content, "Server Answer", JOptionPane.INFORMATION_MESSAGE);
		}
		return false;
	}

}
